import {request} from "../../request/index"
import regeneratorRuntime, { async } from '../../lib/runtime/runtime.js'
Page({
  data: {
    leftMenuList:[],
    rightContent:[],
    scrollTop:0,
    currentIndex:0
  },
  onLoad(){
    this.getCates();
  },
  handleItmeTap(e){
    console.log(e);
    this.setData({
      currentIndex:e.currentTarget.data.set.index
    })
  },
  async getCates(){
    wx.showLoading({
      title: '加载中',
      mask:true
    })

    var cates=wx.getStorageSync('cates');
    console.log(cates);
    if(!cates){//向后台发送请求
      var res= await request({
        url:'/categories'
      })
      this.setData({
              leftMenuList:res,
              rightContent:res[0].children
            })
            console.log(res);
            wx.setStorageSync('cates',{time:Date.now(),data:res})

      // wx.request({
      //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/categories',
      //   success:res=>{
      //     this.setData({
      //       leftMenuList:res.data.message,
      //       rightContent:res.data.message[0].children
      //     })
      //     console.log(res);
      //     wx.setStorageSync('cates',{time:Date.now(),data:res.data.message})
      //   }
      // })
    }else{
      console.log(Date.now() - cates.time);
      if(Date.now() - cates.time>1000*10){
        // wx.request({
        //   url: '/categories',
        //   success:res=>{
        //     this.setData({
        //       leftMenuList:res.data.message,
        //       rightContent:res.data.message[0].children
        //     })
        //   }
        // })
        var res= await request({
          url:'/categories'
        })
        this.setData({
          leftMenuList:res,
          rightContent:res[0].children
        })
        wx.setStorageSync('cates',{time:Date.now(),data:res})
      }else{
          this.setData({
          leftMenuList:cates.data,
          rightContent:cates.data[0].children
        })
        wx.hideLoading();
      }
      
    }
  }
})